<template>
  <div>
    <button @click="openBluetoothAdapter">初始化蓝牙模块</button>
    <button @click="startBluetoothDevicesDiscovery">搜索蓝牙并监听</button>
    <button @click="stopBluetoothDevicesDiscovery">停止搜索</button>
    <button @click="getConnectedBluetoothDevices">获取已连接设备的did</button>
    <button @click="getBluetoothDevices">获取已发现设备</button>
    <button @click="getBluetoothAdapterState">获取蓝牙状态</button>
    <button @click="closeBluetoothAdapter">关闭蓝牙模块</button>
    <input type="number" placeholder="输入设备的deviceID" v-model="i">
    <button @click="createBLEConnection">根据did-连接-BLE并监听</button>
    <button @click="getBLEDeviceServices">根据did获取服务id</button>
    <button @click="getBLEDeviceCharacteristics">根据sid获取特征值id</button>
    <button @click="notifyBLECharacteristicValueChange">根据cid启动notify功能并监听</button>
    <button @click="writeBLECharacteristicValue">根据cid写入特征值数据</button>
    <button @click="readBLECharacteristicValue">根据cid读取特征值数据</button>
    <button @click="closeBLEConnection">关闭低功耗蓝牙连接</button>
  </div>
</template>

<script>
export default {
  data() {
    return {
      d: [], //存放所有已发现设备
      s: [], //服务ID
      c: [], //特征值ID
      i: 0,
      did: null,
      sid: null,
      cid: null
    };
  },
  methods: {
    //初始化 蓝牙模块  若蓝牙不可用请先打开 中心设备蓝牙
    openBluetoothAdapter() {
      console.log("进入初始化模块：");
      wx.openBluetoothAdapter({
        success: res => {
          console.log("初始化蓝牙模块：成功");
          console.log(res);
        },
        fail: res => {
          console.log("初始化蓝牙模块：失败");
          console.log(res);
          if (res.errCode === 10001) {
            console.log("蓝牙未打开，请先打开蓝牙");
            wx.onBluetoothAdapterStateChange(res => {
              console.log("监听蓝牙状态 改变");
              console.log(res);
              if (res.available) {
                console.log("蓝牙设备已经打开,请继续搜索外围设备 操作");
              } else if (res.available == false) {
                console.log("蓝牙设备再次关闭，通信过程请保持打开");
              }
            });
          }
        }
      });
    },
    //搜索外围设备蓝牙，并监听
    startBluetoothDevicesDiscovery() {
      console.log("进入搜索模块：");
      wx.startBluetoothDevicesDiscovery({
        success: res => {
          console.log("搜索成功");
          console.log(res);
          /*  wx.getBluetoothDevices({
            success: res => {
              console.log("获取 所有已发现设备");
              console.log(res);
            }
          }); */
          wx.onBluetoothDeviceFound(devices => {
            console.log("获取到的外围设备：");
            console.log(devices);
          });
        }
      });
    },
    //停止搜索，注：找到外围设备并连接后停止搜索
    stopBluetoothDevicesDiscovery() {
      wx.stopBluetoothDevicesDiscovery({
        success: res => {
          console.log("停止搜索，释放资源");
          console.log(res);
        }
      });
    },
    //获取 已连接设备
    getConnectedBluetoothDevices() {
      wx.getConnectedBluetoothDevices({
        services: [],
        success: res => {
          console.log("获取已连接设备ID");
          console.log(res);
        }
      });
    },
    //获取 已发现设备
    getBluetoothDevices() {
      wx.getBluetoothDevices({
        success: res => {
          console.log("获取 所有已发现设备");
          console.log(res);
          // 把 所有已经发现的设备 放入d 空数组中
          this.d = res.devices;
          console.log("已发现的设备对象，存放到数组中");
          console.log(this.d);
        }
      });
    },
    // 获取 蓝牙适配器状态
    getBluetoothAdapterState() {
      wx.getBluetoothAdapterState({
        success: res => {
          console.log("获取蓝牙适配器状态");
          console.log(res);
        }
      });
    },
    // 关闭 蓝牙模块
    closeBluetoothAdapter() {
      wx.closeBluetoothAdapter({
        success: res => {
          console.log("关闭蓝牙适配模块");
          console.log(res);
        }
      });
    },
    // 根据 设备id连接 外围设备
    createBLEConnection() {
      this.did = this.d[this.i].deviceId;
      console.log("设备Id：" + this.did);
      wx.createBLEConnection({
        deviceId: this.did,
        success: res => {
          console.log("连接外围设备成功");
          console.log("设备号为：" + this.did);
          console.log(res);
          wx.onBLEConnectionStateChange(res => {
            console.log("监听连接触发：");
            console.log(res);
          });
        },
        fail: res => {
          console.log("连接设备失败！");
          console.log(res);
        },
        complete: res => {
          console.log("153行");
          console.log(res);
        }
      });
    },
    // 根据 设备id 获取外围设备所有服务
    getBLEDeviceServices() {
      wx.getBLEDeviceServices({
        deviceId: this.did,
        success: res => {
          console.log("获取到所有的设备服务：");
          console.log(res);
          //把所有的服务放入 s 数组总
          this.s = res.services;
        },
        fail: res => {
          console.log("获取到服务失败：");
          console.log(res);
        }
      });
    },
    // 根据 sid 获取所有的 特征值
    getBLEDeviceCharacteristics() {
      this.sid = this.s[this.i].uuid;
      console.log(this.sid);
      wx.getBLEDeviceCharacteristics({
        deviceId: this.did,
        serviceId: this.sid,
        success: res => {
          console.log("获取所有特征值成功");
          console.log(res);
          // 把所有的特征值 放入c数组中
          this.c = res.characteristics;
        },
        fail: res => {
          console.log("获取所有特征值失败");
          console.log(res);
        }
      });
    },
    //根据 cid 启动notify 功能 并监听
    notifyBLECharacteristicValueChange() {
      this.cid = this.c[this.i].uuid;
      wx.notifyBLECharacteristicValueChange({
        deviceId: this.did,
        serviceId: this.sid,
        characteristicId: this.cid,
        state: true,
        success: res => {
          console.log("notify开启成功：");
          console.log(res);
          wx.onBLECharacteristicValueChange(res => {
            console.log("监听到低功耗蓝牙设备的特征值改变");
            console.log(res);
          });
        }
      });
    },
    // 根据 cid 写入 二进制数据
    writeBLECharacteristicValue() {
      let buffer =new ArrayBuffer(1)
      let dataView=new DataView(buffer,0)
      dataView.setInt8(0,5)
      wx.writeBLECharacteristicValue({
        deviceId: this.did,
        serviceId: this.sid,
        characteristicId: this.cid,
        value:buffer,
        complete:res=>{
          console.log('写入特征值数据：')
          console.log(res)
        }
      });
    },
    //根据 cid 读取特征值数据
    readBLECharacteristicValue(){
       wx.readBLECharacteristicValue({
        deviceId: this.did,
        serviceId: this.sid,
        characteristicId: this.cid,
        complete:res=>{
          console.log('读取特征值数据：')
          console.log(res)
        }
      })
    },
    //根据 did 断开连接
    closeBLEConnection(){
      wx. closeBLEConnection({
         deviceId: this.did,
         success:res=>{
           console.log('已断开设备连接')
           console.log(res)
         }
      })
    }
  }
};
</script>

<style scoped>
</style>
